package com.java.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;


import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;




/*
* 项目思路: hdfs下载文件excel,进行数据的处理
*
* */
/**
 * @Author: ZhangJin
 * @Date: 2020/8/23 18:45
 */
@Slf4j
public class HdfsUtil {


    /**
     *@Description:
     *@Parameter:[fileName, filePathOfHdfs, filePathOfWindows]  fileName --> 上传的文件名 filePathOfHdfs --> Hdfs文件路径 filePathOfWindows --> Windows文件路径
     *@Return:void
     *@Author:ZhangJin
     *@Date:2020/8/23
     **/
    public static void downFile(String fileName, String filePathOfHdfs, String filePathOfWindows) throws URISyntaxException, IOException, InterruptedException {
        FileSystem fileSystem = null;
        Configuration configuration = new Configuration();
        fileSystem = FileSystem.get(new URI("hdfs://192.168.10.3:9000"), configuration, "root");
        InputStream input = null;
        OutputStream output = null;
        try {
            input = fileSystem.open(new Path(filePathOfHdfs + fileName));
            output = new FileOutputStream( filePathOfWindows+fileName);
            IOUtils.copyBytes(input, output, 4096, true);
//            log.info("下载成功.");
        } catch (Exception e) {
//          log.error("下载失败.");
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                }
            }
            if (output != null) {
                try {
                    output.close();
                } catch (IOException e) {
                }
            }
        }
    }


  /**
   *@Description:
   *@Parameter:[inputFilePath, outputFilePath]  inputFilePath --> 本地windows系统  outputFilePath --> hadoop集群hdfs系统
   *@Return:void
   *@Author:ZhangJin
   *@Date:2020/8/23
   **/
    private static void upLoadFileToHdfs(String inputFilePath,String outputFilePath) throws IOException {
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://192.168.10.3:9000");
        FileSystem fs = FileSystem.get(conf);
        fs.copyFromLocalFile(new Path(inputFilePath), new Path(outputFilePath));
        fs.close();
    }


}
